package com.nstar.countdown.view.main.impl
{
	import com.jwopitz.containers.TileCanvas;
	import com.nstar.base.controls.AlertFX;
	import com.nstar.base.event.AdvanceEvent;
	import com.nstar.base.event.RequestEvent;
	import com.nstar.base.util.LoadXMLDelegate;
	import com.nstar.base.util.Logger;
	import com.nstar.base.util.SystemUtils;
	import com.nstar.countdown.controller.CountDownManager;
	import com.nstar.countdown.model.vo.CountDownVO;
	import com.nstar.countdown.view.components.CountDownPanelSimple;
	import com.nstar.countdown.view.main.CountDownEditor;
	
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.utils.setInterval;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.ViewStack;
	import mx.controls.Button;
	import mx.controls.LinkBar;
	import mx.core.Application;
	import mx.core.ClassFactory;
	import mx.events.FlexEvent;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;

	public class CountDownClass extends Application
	{
		public var logger:Logger = Logger.getInstance();
		
		public var tcTop:TileCanvas;
		public var tcLoader:TileCanvas;
		public var lbTraditional:LinkBar;
		public var btnSearch:Button;
		public var btnBack:Button;
		public var btnLog:Button;
		public var vsMain:ViewStack;
		public var cavRandom:Canvas;
		public var btnAdd:Button;
		
		[Bindable] var list:ArrayCollection = new ArrayCollection();
		
		private var uCreateInteval:uint ;
		private var uRemoveInteval:uint ;
		
		public function CountDownClass()
		{
			//TODO: implement function
			super();
			this.addEventListener(FlexEvent.CREATION_COMPLETE,init);
		}
		
		private function init(event:FlexEvent):void{
			btnSearch.addEventListener(MouseEvent.CLICK,searchHandler);
			btnBack.addEventListener(MouseEvent.CLICK,backHandler);
			btnLog.addEventListener(MouseEvent.CLICK,showLog);
			btnAdd.addEventListener(MouseEvent.CLICK,showEditor);
			
			randomChild();
		}
		
		private function load():void{
			var delegateTop : LoadXMLDelegate = new LoadXMLDelegate(new RespondTraditional(refreshTop),
				'demo/data/top.xml');
			delegateTop.load();
			
			var delegate : LoadXMLDelegate = new LoadXMLDelegate(new RespondTraditional(refresh), 
				'demo/data/countdown.xml');
			delegate.load();			
		}
		
		private function searchHandler(event:MouseEvent):void{
			vsMain.selectedIndex =1;
			tcLoader.dataProvider = list;
		}
		
		private function backHandler(event:MouseEvent):void{
			if (vsMain.selectedIndex ==0){
				vsMain.selectedIndex = 1;
			}else{
				vsMain.selectedIndex = 0;
			}
		}
		
		private function showLog(event:MouseEvent):void{
			vsMain.selectedIndex = 2;
		}
		
		private function showEditor(event:MouseEvent):void{
			var cde:CountDownEditor =  CountDownEditor.show();
			cde.addEventListener("addCountDown",addCountDownHandler);
		}
		
		private function addCountDownHandler(event:RequestEvent):void{
			var dp:ArrayCollection = tcTop.dataProvider as ArrayCollection;
			dp[dp.length-1]=event.request;
			tcTop.dataProvider = dp;
			startTop(dp);
		}
		
		public function refreshTop(data:Object):void{
			var list:ArrayCollection = data.result.dataconfig.item;
			tcTop.dataProvider = list;
			startTop(list);
		}
		
		private function startTop(list:ArrayCollection):void{
			for(var i:int=0;i<list.length;i++){
				var item:Object = tcTop.getChildAt(i);
				item.start();
			}	
		}
		
		public function refresh(data:Object):void{
			list = data.result.dataconfig.item;
			tcLoader.validateNow();
			var menus:Array = new Array();
			for(var i:int=0;i<list.length;i++){
				var item:Object = list[i];
				item.label = item["mainTitle"];
				menus.push(item);
			}			
			lbTraditional.dataProvider = menus;
			 
		}
		
		public function setupRenderer(className:Class):ClassFactory{
			var cf:ClassFactory = new ClassFactory(className);
			return cf;
		}	
		
		public function tcCreationComplete ():void {
			tcLoader.direction = 'vertical';
			tcLoader.itemSpawnPoint = new Point(tcLoader.width - tcLoader.columnWidth, 
				tcLoader.height - tcLoader.rowHeight);
		}	
		
		//随机添加
		private function randomChild():void{
			var maxChild:int = 5;//最多可以添加的子组件数目
			var time2Dispear:int = 1 ;//多长时间内开始消失，秒为单位
			uCreateInteval = setInterval(addRandomChild,time2Dispear*1000,cavRandom,CountDownPanelSimple);
			uRemoveInteval = setInterval(removeLast,time2Dispear*1000,cavRandom,maxChild);
		}
		
		//在owner的随机位置添加child
		private function addRandomChild(owner:Canvas,child:Class):void{
			if(list.length>0){
				var inst:CountDownPanelSimple = new child();
				var rndNum:int = SystemUtils.randRange(0,list.length - 1);
				inst.vo = new CountDownVO(list[rndNum].logo,null,list[rndNum].mainTitle,null,null,
					list[rndNum].url,list[rndNum].info,new Date(list[rndNum].date));
				
				var childInst:Object = owner.addChildAt(inst,owner.numChildren);
				childInst.x = SystemUtils.randRange(0,owner.width - childInst.width - 2);
				childInst.y = SystemUtils.randRange(0,owner.height - childInst.height - 2);
			}
		}	
		//去掉第一个，保持owner里最多有maxChild个
		private function removeLast(owner:Canvas,maxChild:int):void{
			if (owner.numChildren>maxChild){
				owner.removeChildAt(0);
			}
		}
	}
}